package com.example.hms.service.serviceImpl;
import com.example.hms.Dao.ReserveRecordsDao;
import com.example.hms.entity.ReserveRecords;
import com.example.hms.service.ReserveRecordsService;
import com.example.hms.ulit.Result;
import com.example.hms.ulit.ResultSubmit;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
public class ReserveRecordsServiceImpl implements ReserveRecordsService {
    @Resource
    ReserveRecordsDao reserveRecordsDao;
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public Result selectAllReserveRecords(String roomNumber, String customerName, String identificationCard) {
        List<ReserveRecords> reserveRecords = reserveRecordsDao.selectAllReserveRecords(roomNumber,customerName,identificationCard);
        return ResultSubmit.success(reserveRecords);
    }
    @Override
    public int insert(ReserveRecords reserveRecords) {
        reserveRecords.setDataUpdateTime(format.format(new Date()));
        reserveRecords.setRoomId(Math.abs((int) System.currentTimeMillis()));
        reserveRecords.setDeleteIdentity("未删除");
        int insert = reserveRecordsDao.insert(reserveRecords);
        if (insert != 0) {
            return insert;
        } else {
            // 事务回滚(手动回滚)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return 0;
    }
    @Override
    public Result update(ReserveRecords reserveRecords) {
        int update1 = reserveRecordsDao.update(reserveRecords);
        reserveRecords.setDataUpdateTime(format.format(new Date()));

        reserveRecords.setRoomId(Math.abs((int)System.currentTimeMillis()));
        reserveRecords.setDeleteIdentity("未删除");
        int insert1 = reserveRecordsDao.insert(reserveRecords);
        if (update1==1 || insert1==1){
            return ResultSubmit.success("修改成功");
        }else {
            // 事务回滚(手动回滚)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("修改失败");
        }
    }
    @Override
    public Result delete(ReserveRecords reserveRecords){
        int delete1 = reserveRecordsDao.update(reserveRecords);
        if (delete1==1){
            return ResultSubmit.success("执行成功");
        }else {
            // 事务回滚(手动回滚)
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResultSubmit.error("执行失败");
        }
    }
}
